package com.gsmr.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.gsmr.entity.FACCH;
import com.gsmr.entity.PageBean;
import com.gsmr.service.FACCHService;
import com.gsmr.util.DateUtil;
import com.gsmr.util.ExcelUtil;
import com.gsmr.util.FormatDate;
import com.gsmr.util.PropertityUtil;
import com.gsmr.util.ResponseUtil;
import com.gsmr.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * FACCH Controller层
 * @author user
 *
 */
@Controller
@RequestMapping("/facch")
public class FACCHController {

	@Resource
	private FACCHService facchService;
	
	List<FACCH> facchList;
	
	/**
	 * 分页条件查询FACCH
	 */
	@RequestMapping("/list")
	public String list(
			@RequestParam(value="page",required=false )String page,
			@RequestParam(value="rows",required=false)String rows,
			@RequestParam(value="startTime",required=false)String startTime,
			@RequestParam(value="endTime",required=false)String endTime,
			@RequestParam(value="IMSI",required=false)String IMSI,
			@RequestParam(value="MSISDN",required=false)String MSISDN,
			@RequestParam(value="CTCS_ID",required=false)String CTCS_ID,
			@RequestParam(value="UmType",required=false)String UmType,
			@RequestParam(value="district",required=false)String district,
			@RequestParam(value="kilometerStart",required=false)String kilometerStart,
			@RequestParam(value="kilometerEnd",required=false)String kilometerEnd,
			@RequestParam(value = "search", required = false) String search,
			FACCH facch,HttpServletResponse response)throws Exception{
		if(search==null){
			return null;
		}
		System.out.println("FACCH startTime:" + startTime);
		System.out.println("FACCH endTime:" + endTime);
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("FrontProcessorId", StringUtil.formatLike(facch.getFrontProcessorId())); // 查询FACCH名获取
		map.put("IMSI", IMSI);
		map.put("MSISDN", MSISDN);
		map.put("CTCS_ID", CTCS_ID);
		map.put("UmType", UmType);
		map.put("district", district);
		map.put("kilometerStart", kilometerStart);
		map.put("kilometerEnd", kilometerEnd);
		map.put("start", pageBean.getStart());
		//当天00:00的时间戳
		Long earlyMorningTime = System.currentTimeMillis()/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
		//当前时间戳
		Long nowTime = System.currentTimeMillis();
		
		//用户若输入时间就按输入时间查天表名，没有就查当天天表名
		if(startTime!=null&&!startTime.equals("NaN")){
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime),"yyyyMMdd"));
		}
		else {
			 map.put("startTime", DateUtil.longToString(earlyMorningTime,"yyyyMMdd"));
		}
		if(endTime!=null&&!endTime.equals("NaN")){
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime),"yyyyMMdd"));
		}
		else {
			map.put("endTime", DateUtil.longToString(nowTime,"yyyyMMdd"));
		}
		
		map.put("size", pageBean.getPageSize());
		
		long findDataStartTime = System.currentTimeMillis();
		
		List<String> table_names = facchService.findTableNames(map);
		if(startTime!=null&&!startTime.equals("NaN")){
			map.put("startTime", startTime);
		}
		else {
			 map.put("startTime", null);
		}
		if(endTime!=null&&!endTime.equals("NaN")){
			map.put("endTime", endTime);
		}
		else {
			map.put("endTime", null);
		}
		facchList = new ArrayList<FACCH>();
		
		if(table_names!=null){
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
			Long curTableSize_long;
			int curTableSize;
			for(int i=0;i<table_names.size()&&facchList.size()<pageBean.getPageSize();i++){
				map.put("table_name", table_names.get(i));
				/*curTableSize_long = facchService.getTotal(map);
				curTableSize = curTableSize_long.intValue();
				if(curTableSize>0){
					if(curTableSize>=(int)map.get("start")+(int)map.get("size")){
						List<FACCH> sub_facchList = facchService.find(map);
						facchList.addAll(sub_facchList);
					}else if(curTableSize>=(int)map.get("start")&&curTableSize<(int)map.get("start")+(int)map.get("size")){
						List<FACCH> sub_facchList = facchService.find(map);
						facchList.addAll(sub_facchList);
						map.put("size", (int)map.get("start")+(int)map.get("size")-curTableSize);
						map.put("start", 0);
					}else if(curTableSize<(int)map.get("start")){
						map.put("start", (int)map.get("start")-curTableSize);
					}
				}*/
				
				List<FACCH> sub_facchList = facchService.find(map);
				curTableSize = sub_facchList.size();
				if(curTableSize==(int)map.get("size")){
					facchList.addAll(sub_facchList);
				}else if(curTableSize<(int)map.get("size")&&curTableSize>0){
					facchList.addAll(sub_facchList);
					map.put("size", (int)map.get("size")-curTableSize);
					map.put("start", 0);
				}
				
				//List<FACCH> sub_facchList = facchService.find(map);
				//facchList.addAll(sub_facchList);
				
			}
		}
		long findDataEndTime = System.currentTimeMillis();
		
		/*Long total = new Long(0);
		if(table_names!=null){
			map.put("start", null);
			map.put("size", null);
			for(int i=0;i<table_names.size();i++){
				map.put("table_name", table_names.get(i));
				Long sub_total = facchService.getTotal(map);
				total = total + sub_total;
			}
		}*/
		
		int total = Integer.MAX_VALUE;
		long findTotalEndTime = System.currentTimeMillis();
		System.out.println("查找"+pageBean.getPageSize()+"条FACCH数据花的时间（毫秒）"+(findDataEndTime-findDataStartTime));
		System.out.println("查找"+pageBean.getPageSize()+"条FACCH数据对应的总量花的时间（毫秒）"+(findTotalEndTime-findDataEndTime));
		
		JSONObject result=new JSONObject();
		JSONArray jsonArray=JSONArray.fromObject(facchList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 判断是否存在指定PrimaryKey
	 * @param userName
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/exisPrimaryKey")
	public String existUserName(String primaryKey,HttpServletResponse response)throws Exception{
		JSONObject result=new JSONObject();
		Map<String,Object> map=new HashMap<String,Object>();
		String frontProcessorId = primaryKey.substring(0,primaryKey.indexOf("-"));
		map.put("table_name", "facch_"+frontProcessorId);
		map.put("PrimaryKey", primaryKey);
		if(facchService.findByPrimaryKey(map)==null){
			result.put("exist", false);
		}else{
			result.put("exist", true);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 添加或者修改FACCH
	 * @param FACCH
	 * @param response
	 * @param flag 1 添加  2修改
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(FACCH facch,HttpServletResponse response,Integer flag)throws Exception{
		int resultTotal=0; // 操作的记录条数
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("table_name", "facch_"+facch.getFrontProcessorId());
		map.put("PrimaryKey", facch.getPrimaryKey());
		map.put("TimeStamp", facch.getTimeStamp());
		map.put("StartTime", facch.getStartTime());
		map.put("FrontProcessorId", facch.getFrontProcessorId());
		map.put("RecordId", facch.getRecordId());
		map.put("IMSI", facch.getTMSI());
		map.put("LogicChannel", facch.getLogicChannel());
		map.put("MSISDN", facch.getMSISDN());
		map.put("Direction", facch.getDirection());
		map.put("FrameNumber", facch.getFrameNumber());
		map.put("SiteName", facch.getSiteName());
		map.put("Decode", facch.getDecode());
		map.put("RawMessage", facch.getRawMessage());
		map.put("Kilometer", facch.getKilometer());
		map.put("Speed", facch.getSpeed());
		map.put("CI", facch.getCI());
		map.put("LAC", facch.getLAC());
		map.put("CTCS_ID", facch.getCTCS_ID());
		map.put("ENGINE_ID", facch.getENGINE_ID());
		map.put("MS_TYPE", facch.getMS_TYPE());
		map.put("Message_TYPE", facch.getMessage_TYPE());
		map.put("SourceData", facch.getSourceData());
		map.put("S_ARFCN", facch.getS_ARFCN());
		map.put("T_BSIC", facch.getT_BSIC());
		map.put("T_BCCH", facch.getT_BCCH());
		map.put("T_ARFCN", facch.getT_ARFCN());
		map.put("HO_Reference", facch.getHO_Reference());
		map.put("TA", facch.getTA());
		map.put("TimeSlot", facch.getTimeSlot());
		map.put("TMSI", facch.getTMSI());
		map.put("Channel", facch.getChannel());
		map.put("TCH", facch.getTCH());
		map.put("IMEI", facch.getIMEI());
		map.put("L2", facch.getL2());
		if(flag==1){
			resultTotal=facchService.add(map);
		}else{
			resultTotal=facchService.update(map);
		}
		JSONObject result=new JSONObject();
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 删除FACCH
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="primaryKeys")String primaryKeys,HttpServletResponse response)throws Exception{
		String []primaryKeysStr=primaryKeys.split(",");
		Map<String,Object> map=new HashMap<String,Object>();
		JSONObject result=new JSONObject();
		try {
			for(int i=0;i<primaryKeysStr.length;i++){
				String frontProcessorId = primaryKeysStr[i].substring(0,primaryKeysStr[i].indexOf("-"));
				map.put("table_name", "facch_"+frontProcessorId);
				map.put("PrimaryKey", primaryKeysStr[i]);
				facchService.delete(map);
			}
			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	
	/**
	 * 导出FACCH
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/export")
	public String export(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		Workbook wb=new HSSFWorkbook();
		String headers[]={"主键","文件入库时间","文件生成时间","前置机号","记录号",
		"IMSI","LogicChannel","MSISDN","方向","FrameNumber",
		"小区号","Decode","源信息","公里标","速度","CI",	
		"LAC","机车号","ENGINE_ID","MS_TYPE","Um接口信令类型",	
		"SourceData","S_ARFCN","T_BSIC","T_BCCH","T_ARFCN",	
		"HO_Reference","TA","TimeSlot","TMSI","Channel",	
		"TCH","IMEI","L2","DataLength","LRBG","CRC_ECC","T_Train"
		};
		if(ids!=null&&!ids.equals("null")&&!ids.equals("")){
			/*List<String> idsList = Arrays.asList(ids.split(","));
			map.put("ids", idsList);
			facchList.clear();
			List<FACCH> sub_facchList = null;
			List<String> table_names = facchService.findTableNames(map);
			if(table_names!=null){
				for (int i = 0; i < table_names.size(); i++) {
					sub_facchList = null;
					map.put("table_name", table_names.get(i));
					sub_facchList = facchService.findByIds(map);
					if (sub_facchList != null) {
						facchList.addAll(sub_facchList);
					}

				}
			}*/
			try {
				ExcelUtil.fillExcelData_FACCH(facchList, wb, headers);
				ResponseUtil.export(response, wb, "导出excel"+FormatDate.formatDate()+".xls");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			List<FACCH> facchList_all = new ArrayList<FACCH>();
			List<FACCH> sub_facchList = null;;
			List<String> table_names = facchService.findTableNames(map);
			if(table_names!=null){
				for(int i=0;i<table_names.size()&&facchList_all.size()<=10000;i++){
					sub_facchList = null;
					map.put("table_name", table_names.get(i));
					sub_facchList = facchService.find(map);
					if(sub_facchList!=null){
						facchList_all.addAll(sub_facchList);
					}
					
				}
			}
			try {
				ExcelUtil.fillExcelData_FACCH(facchList_all, wb, headers);
				ResponseUtil.export(response, wb, "导出excel"+FormatDate.formatDate()+".xls");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 用模板导出用户
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/exportByTemplate")
	public String exportByTemplate(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		List<FACCH> facchList = new ArrayList<FACCH>();
		if(ids!=null&&!ids.equals("null")&&!ids.equals("")){
			List<String> idsList = Arrays.asList(ids.split(","));
			map.put("ids", idsList);
			facchList.clear();
			List<FACCH> sub_facchList = null;
			List<String> table_names = facchService.findTableNames(map);
			if(table_names!=null){
				for (int i = 0; i < table_names.size(); i++) {
					sub_facchList = null;
					map.put("table_name", table_names.get(i));
					sub_facchList = facchService.findByIds(map);
					if (sub_facchList != null) {
						facchList.addAll(sub_facchList);
					}

				}
			}
			try {
				Workbook wb=ExcelUtil.fillExcelDataWithTemplate_FACCH(facchList, "facchExportTemplate.xls");
				ResponseUtil.export(response, wb, "利用模版导出excel"+FormatDate.formatDate()+".xls");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			facchList.clear();
			List<FACCH> sub_facchList = null;
			List<String> table_names = facchService.findTableNames(map);
			if(table_names!=null){
				for(int i=0;i<table_names.size();i++){
					sub_facchList = null;
					map.put("table_name", table_names.get(i));
					sub_facchList = facchService.find(map);
					facchList.addAll(sub_facchList);
				}
			}
			try {
				Workbook wb=ExcelUtil.fillExcelDataWithTemplate_FACCH(facchList, "facchExportTemplate.xls");
				ResponseUtil.export(response, wb, "利用模版导出excel"+FormatDate.formatDate()+".xls");
			} catch (Exception e) {
				e.printStackTrace();
			}		
		}
		return null;
	}
	
	/**
	 * 用户上传
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/upload")
	public String upload(HttpServletRequest request,HttpServletResponse response)throws Exception{
		JSONObject result=new JSONObject();
		Map<String,Object> map=new HashMap<String,Object>();
		 try{
        //将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
       CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
       //检查form中是否有enctype="multipart/form-data"
       if(multipartResolver.isMultipart(request))
       {
           //将request变成多部分request
           MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
          //获取multiRequest 中所有的文件名
           Iterator iter=multiRequest.getFileNames();
           
          
           while(iter.hasNext())
           {
               //一次遍历所有文件
               MultipartFile file=multiRequest.getFile(iter.next().toString());
               if(file!=null)
               {
                   String path=PropertityUtil.UPLOAD_PATH+FormatDate.formatDate()+file.getOriginalFilename();
                   //上传
                   File newFile = new File(path);
                   file.transferTo(newFile);
                   //写数据进数据库
                   POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(newFile));
              		HSSFWorkbook wb=new HSSFWorkbook(fs);
              		HSSFSheet hssfSheet=wb.getSheetAt(0);  // 获取第一个Sheet页
              			if(hssfSheet!=null){
              				for(int rowNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){
              					HSSFRow hssfRow=hssfSheet.getRow(rowNum);
              					if(hssfRow==null){
              						continue;
              					}
              					FACCH facch=new FACCH();
								facch.setPrimaryKey(ExcelUtil.formatCell(hssfRow.getCell(0)));
								facch.setTimeStamp(ExcelUtil.formatCell(hssfRow.getCell(1)));
								facch.setStartTime(ExcelUtil.formatCell(hssfRow.getCell(2)));
								facch.setFrontProcessorId(ExcelUtil.formatCell(hssfRow.getCell(3)));
								facch.setRecordId(ExcelUtil.formatCell(hssfRow.getCell(4)));
								facch.setIMSI(ExcelUtil.formatCell(hssfRow.getCell(5)));
								facch.setLogicChannel(ExcelUtil.formatCell(hssfRow.getCell(6)));
								facch.setMSISDN(ExcelUtil.formatCell(hssfRow.getCell(7)));
								facch.setDirection(ExcelUtil.formatCell(hssfRow.getCell(8)));
								facch.setFrameNumber(ExcelUtil.formatCell(hssfRow.getCell(9)));
								facch.setSiteName(ExcelUtil.formatCell(hssfRow.getCell(10)));
								facch.setDecode(ExcelUtil.formatCell(hssfRow.getCell(11)));
								facch.setRawMessage(ExcelUtil.formatCell(hssfRow.getCell(12)));
								facch.setKilometer(ExcelUtil.formatCell(hssfRow.getCell(13)));
								facch.setSpeed(ExcelUtil.formatCell(hssfRow.getCell(14)));
								facch.setCI(ExcelUtil.formatCell(hssfRow.getCell(15)));
								facch.setLAC(ExcelUtil.formatCell(hssfRow.getCell(16)));
								facch.setCTCS_ID(ExcelUtil.formatCell(hssfRow.getCell(17)));
								facch.setENGINE_ID(ExcelUtil.formatCell(hssfRow.getCell(18)));
								facch.setMS_TYPE(ExcelUtil.formatCell(hssfRow.getCell(19)));
								facch.setMessage_TYPE(ExcelUtil.formatCell(hssfRow.getCell(20)));
								facch.setSourceData(ExcelUtil.formatCell(hssfRow.getCell(21)));
								facch.setS_ARFCN(ExcelUtil.formatCell(hssfRow.getCell(22)));
								facch.setT_BSIC(ExcelUtil.formatCell(hssfRow.getCell(23)));
								facch.setT_BCCH(ExcelUtil.formatCell(hssfRow.getCell(24)));
								facch.setT_ARFCN(ExcelUtil.formatCell(hssfRow.getCell(25)));
								facch.setHO_Reference(ExcelUtil.formatCell(hssfRow.getCell(26)));
								facch.setTA(ExcelUtil.formatCell(hssfRow.getCell(27)));
								facch.setTimeSlot(ExcelUtil.formatCell(hssfRow.getCell(28)));
								facch.setTMSI(ExcelUtil.formatCell(hssfRow.getCell(29)));
								facch.setChannel(ExcelUtil.formatCell(hssfRow.getCell(30)));
								facch.setTCH(ExcelUtil.formatCell(hssfRow.getCell(31)));
								facch.setIMEI(ExcelUtil.formatCell(hssfRow.getCell(32)));
								facch.setL2(ExcelUtil.formatCell(hssfRow.getCell(33)));
								
								map.put("table_name", "facch_"+facch.getFrontProcessorId());
								map.put("PrimaryKey", facch.getPrimaryKey());
								map.put("TimeStamp", facch.getTimeStamp());
								map.put("StartTime", facch.getStartTime());
								map.put("FrontProcessorId", facch.getFrontProcessorId());
								map.put("RecordId", facch.getRecordId());
								map.put("IMSI", facch.getTMSI());
								map.put("LogicChannel", facch.getLogicChannel());
								map.put("MSISDN", facch.getMSISDN());
								map.put("Direction", facch.getDirection());
								map.put("FrameNumber", facch.getFrameNumber());
								map.put("SiteName", facch.getSiteName());
								map.put("Decode", facch.getDecode());
								map.put("RawMessage", facch.getRawMessage());
								map.put("Kilometer", facch.getKilometer());
								map.put("Speed", facch.getSpeed());
								map.put("CI", facch.getCI());
								map.put("LAC", facch.getLAC());
								map.put("CTCS_ID", facch.getCTCS_ID());
								map.put("ENGINE_ID", facch.getENGINE_ID());
								map.put("MS_TYPE", facch.getMS_TYPE());
								map.put("Message_TYPE", facch.getMessage_TYPE());
								map.put("SourceData", facch.getSourceData());
								map.put("S_ARFCN", facch.getS_ARFCN());
								map.put("T_BSIC", facch.getT_BSIC());
								map.put("T_BCCH", facch.getT_BCCH());
								map.put("T_ARFCN", facch.getT_ARFCN());
								map.put("HO_Reference", facch.getHO_Reference());
								map.put("TA", facch.getTA());
								map.put("TimeSlot", facch.getTimeSlot());
								map.put("TMSI", facch.getTMSI());
								map.put("Channel", facch.getChannel());
								map.put("TCH", facch.getTCH());
								map.put("IMEI", facch.getIMEI());
								map.put("L2", facch.getL2());
              					facchService.add(map);
              				}
              			}
               }
           }
       }
       result.put("success", "true");
       } catch (Exception e) {
 			result.put("success", "false");
 			e.printStackTrace();
 		}
 		ResponseUtil.write(response, result);
		return null;
	}
	
	
	/**
	 * 查找District列表
	 * @param 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/facchDistrictComboList")
	public String facchDistrictComboList(HttpServletResponse response)throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		JSONArray jsonArray=new JSONArray();
		JSONObject jsonObject=new JSONObject();
		//System.out.println("开始找sitename" );
		jsonObject.put("id", -1);
		jsonObject.put("name", "");
		jsonArray.add(jsonObject);
		map.put("table_name","bs_info");
		List<String> bs_names=facchService.findDistricts(map);
		for(int i=0;i<bs_names.size();i++)
		{
			jsonObject.put("id", i);
			//String bs_name=alarmInfoService.get_bs_names();
			//System.out.println("bs_names:" + bs_names.get(i));
			jsonObject.put("name",bs_names.get(i));
			jsonArray.add(jsonObject);
			
		}
		ResponseUtil.write(response, jsonArray);
		return null;
		/*JSONArray jsonArray=new JSONArray();
		Map<String,Object> map=new HashMap<String,Object>();
		List<String> table_names = facchService.findTableNames(map);
		List<FACCH> facchList = new ArrayList<FACCH>();
		if(table_names!=null){
			for(int i=0;i<table_names.size();i++){
				map.put("table_name", table_names.get(i));
				List<FACCH> sub_facchList = facchService.findDistricts(map);
				facchList.addAll(sub_facchList);
			}
		}

		// 过滤重复的SiteName
		Map siteName_map = new HashMap<>();
		for (int i = 0; i < facchList.size(); i++) {
			siteName_map.put(facchList.get(i).getSiteName(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = siteName_map.entrySet().iterator();
		facchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			FACCH facch = new FACCH();
			facch.setSiteName(entry.getKey());
			facchList.add(facch);
		}

		JSONArray rows=JSONArray.fromObject(facchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		return null;*/
	}
	
	/**
	 * 查找Message_TYPE列表
	 * @param 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/facchMessage_TYPEComboList")
	public String facchMessage_TYPEComboList(HttpServletResponse response)throws Exception{
		JSONArray jsonArray=new JSONArray();
		Map<String,Object> map=new HashMap<String,Object>();
		List<String> table_names = facchService.findTableNames(map);
		List<FACCH> facchList = new ArrayList<FACCH>();
		if(table_names!=null){
			for(int i=0;i<table_names.size();i++){
				map.put("table_name", table_names.get(i));
				List<FACCH> sub_facchList = facchService.findMessage_TYPEs(map);
				facchList.addAll(sub_facchList);
			}
		}
		
		// 过滤重复的Message_TYPE
		Map message_TYPE_map = new HashMap<>();
		for (int i = 0; i < facchList.size(); i++) {
			message_TYPE_map.put(facchList.get(i).getMessage_TYPE(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = message_TYPE_map.entrySet().iterator();
		facchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			FACCH facch = new FACCH();
			facch.setMessage_TYPE(entry.getKey());
			facchList.add(facch);
		}
				
		JSONArray rows=JSONArray.fromObject(facchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		return null;
	}
	
	/**
	 * 查找前置机列表
	 * @param 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/facchFrontProcessorIdComboList")
	public String facchFrontProcessorIdComboList(HttpServletResponse response)throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		JSONArray jsonArray=new JSONArray();
		JSONObject jsonObject=new JSONObject();
		//System.out.println("开始找sitename" );
		jsonObject.put("id", -1);
		jsonObject.put("name", "");
		jsonArray.add(jsonObject);
		map.put("table_name","bs_info");
		List<String> FrontProcessorIds=facchService.findFrontProcessorIds(map);
		for(int i=0;i<FrontProcessorIds.size();i++)
		{
			jsonObject.put("id", i);
			//String bs_name=alarmInfoService.get_bs_names();
			//System.out.println("FrontProcessorIds:" + FrontProcessorIds.get(i));
			jsonObject.put("name",FrontProcessorIds.get(i));
			jsonArray.add(jsonObject);
			
		}
		ResponseUtil.write(response, jsonArray);
		return null;
		/*JSONArray jsonArray=new JSONArray();
		Map<String,Object> map=new HashMap<String,Object>();
		List<String> table_names = facchService.findTableNames(map);
		List<FACCH> facchList = new ArrayList<FACCH>();
		if(table_names!=null){
			for(int i=0;i<table_names.size();i++){
				map.put("table_name", table_names.get(i));
				List<FACCH> sub_facchList = facchService.findFrontProcessorIds(map);
				facchList.addAll(sub_facchList);
			}
		}
		
		// 过滤重复的FrontProcessorId
		Map frontProcessorId_map = new HashMap<>();
		for (int i = 0; i < facchList.size(); i++) {
			frontProcessorId_map.put(facchList.get(i).getFrontProcessorId(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = frontProcessorId_map.entrySet().iterator();
		facchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			FACCH facch = new FACCH();
			facch.setFrontProcessorId(entry.getKey());
			facchList.add(facch);
		}
				
		JSONArray rows=JSONArray.fromObject(facchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		return null;*/
	}
	
	/**
	 * 查找IMSI列表
	 * @param 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/facchIMSIComboList")
	public String facchIMSIComboList(HttpServletResponse response)throws Exception{
		JSONArray jsonArray=new JSONArray();
		Map<String,Object> map=new HashMap<String,Object>();
		List<String> table_names = facchService.findTableNames(map);
		List<FACCH> facchList = new ArrayList<FACCH>();
		if(table_names!=null){
			for(int i=0;i<table_names.size();i++){
				map.put("table_name", table_names.get(i));
				List<FACCH> sub_facchList = facchService.findIMSIs(map);
				facchList.addAll(sub_facchList);
			}
		}
		
		// 过滤重复的IMSI
		Map iMSI_map = new HashMap<>();
		for (int i = 0; i < facchList.size(); i++) {
			iMSI_map.put(facchList.get(i).getIMSI(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = iMSI_map.entrySet().iterator();
		facchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			FACCH facch = new FACCH();
			facch.setIMSI(entry.getKey());
			facchList.add(facch);
		}
				
		JSONArray rows=JSONArray.fromObject(facchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		return null;
	}
	
	/**
	 * 查找MSISDN列表
	 * @param 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/facchMSISDNComboList")
	public String facchMSISDNComboList(HttpServletResponse response)throws Exception{
		JSONArray jsonArray=new JSONArray();
		Map<String,Object> map=new HashMap<String,Object>();
		List<String> table_names = facchService.findTableNames(map);
		List<FACCH> facchList = new ArrayList<FACCH>();
		if(table_names!=null){
			for(int i=0;i<table_names.size();i++){
				map.put("table_name", table_names.get(i));
				List<FACCH> sub_facchList = facchService.findMSISDNs(map);
				facchList.addAll(sub_facchList);
			}
		}
		
		// 过滤重复的MSISDN
		Map mSISDN_map = new HashMap<>();
		for (int i = 0; i < facchList.size(); i++) {
			mSISDN_map.put(facchList.get(i).getMSISDN(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = mSISDN_map.entrySet().iterator();
		facchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			FACCH facch = new FACCH();
			facch.setMSISDN(entry.getKey());
			facchList.add(facch);
		}
				
		JSONArray rows=JSONArray.fromObject(facchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		return null;
	}
	
	/**
	 * 查找CTCS_ID列表
	 * @param 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/facchCTCS_IDComboList")
	public String facchCTCS_IDComboList(HttpServletResponse response)throws Exception{
		JSONArray jsonArray=new JSONArray();
		Map<String,Object> map=new HashMap<String,Object>();
		List<String> table_names = facchService.findTableNames(map);
		List<FACCH> facchList = new ArrayList<FACCH>();
		if(table_names!=null){
			for(int i=0;i<table_names.size();i++){
				map.put("table_name", table_names.get(i));
				List<FACCH> sub_facchList = facchService.findCTCS_IDs(map);
				facchList.addAll(sub_facchList);
			}
		}
		
		// 过滤重复的CTCS_ID
		Map cTCS_ID_map = new HashMap<>();
		for (int i = 0; i < facchList.size(); i++) {
			cTCS_ID_map.put(facchList.get(i).getCTCS_ID(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = cTCS_ID_map.entrySet().iterator();
		facchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			FACCH facch = new FACCH();
			facch.setCTCS_ID(entry.getKey());
			facchList.add(facch);
		}
				
		JSONArray rows=JSONArray.fromObject(facchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		return null;
	}
}
